<?php

/**
 * @Author: Kendy
 * @Date:   2019-10-22 10:34:15
 * @Last Modified by:   jiayanting
 * @Last Modified time: 2020-04-28 09:48:50
 */
namespace app\api\model; 
use think\Model;
use think\Db; 
class GroupTour extends Model
{
  /**
   * [GetGroupTourList 根据条件获取跟团产品列表]
   * @Author   Kendy
   * @DateTime 2019-10-22T14:57:40+0800
   * @param    [type]                   $where     [条件]
   * @param    [type]                   $page      [当前页面]
   * @param    [type]                   $limit     [每页显示条数，默认为配置里面的]
   */
  public function GetGroupTourList($where,$where1='',$page,$limit='',$orderBy='')
  { 
  	  
       $pageBy = ['sale_sum','gtour_price','app_sum']; 
       $pageOrder = ['asc','desc'];  
       if($where1)
       {
         $resultIdArr = Db::name('gtour_gschedule')
         ->field('tg_id') 
         ->where($where1) 
         ->group('tg_id')
         ->select(); 
         if(count($resultIdArr)>0)
         {
            foreach ($resultIdArr as $k => $v) {
                 $where['id'] = $v['tg_id'];
            }
         }
         else{
           $where['id'] = 0; 
       }
        } 
       $order = ($orderBy==1)?0:1;  
       $orderBy = ($orderBy>=0 && $orderBy<=2 & $orderBy!='')?$orderBy:0;  
       $limit = ($limit==='')?config('page_size'):$limit; 
       $res =  Db::name('group_tour')
            ->field(' id,gtour_covers,gtours_type,gtour_days,gtour_title,gtour_label,gtour_price,app_sum,sale_sum')
            ->where($where)
            ->order($pageBy[$orderBy],$pageOrder[$order])
            ->limit($page,$limit)
            ->select(); 
        if(count($res)>0)
        {
        	foreach ($res as $key => $value) {
            $res[$key]['gtour_covers'] = addWaterByField($value['gtour_covers']);
        		$res[$key]['gtour_label'] = explode(',', $value['gtour_label']);
            $res[$key]['gtour_days'] = numberToChinese($value['gtours_type']);
        	}
            return $res;
        } 
       else return [];
  }



  /**
   * [getGroupTourInfo 获取跟团的详情]
   * @Author   Kendy
   * @DateTime 2019-10-23T08:56:28+0800
   * @param    [int]                   $gt_id [条件]
   * @return   [arr]                          [description]
   */
  public function getGroupTourInfo($gt_id)
  {
  	 $where['is_del'] = 0;  
     $where['id'] = $gt_id;
     $rs = Db::name('group_tour')
     ->field('id as gt_id,team_id,gtour_title,gtour_covers,gtour_label,gtour_price,sale_sum,lv_days,lv_nghit,start')
     ->where($where)
     ->find(); 
     if(!empty($rs))
     {
         $group['is_del'] = 0; 
         $rss = Db::name('gtour_tour_img') 
          ->field('img_path')
          ->where(['gt_id'=>$rs['gt_id'],'is_del'=>0])
          ->select();
          $rs['gt_images'] = [];
          if(count($rss)>0)
          {
            foreach ($rss as $key => $value) {
              $rs['gt_images'][] = addWaterByField($value['img_path']);
            }
          } 
         $rs['gtour_covers'] = addWaterByField($rs['gtour_covers']);
         $gtour_label = [];
         if(!empty($rs['gtour_label']))
         $gtour_label = explode(',', $rs['gtour_label']);
         $rs['gtour_label'] = $gtour_label;
       	 $field = 'team_icon,team_name,team_start_time,team_end_time,team_phone,longitude,latitude';
       	 //获取旅行社的信息
         $teamLists = model('TeamModel')->getTeamInfoById((int)$rs['team_id'],$field);
         if(empty($teamLists)){
             $rs['team'] = "{}";
         }else{
            $rs['team'] = $teamLists;
         }
        //获取行程
         $rs['journey'] = model('GtourJourneyModel')->getGtourJourneyList((int)$rs['gt_id']);
         ///获取该景点的最近一条好评
         $rs['app'] = model('AppraiseModel')->getAppList(3,(int)$rs['gt_id'],1,1,1); 
         //评价个数  
         $rs['count'] =model('AppraiseModel')->getAppCountByItemId(3,(int)$rs['gt_id']);
         //推荐跟团
         $rs['recommend'] =  $this->GetGroupTourList($group,0,4,0);
          
     }
    return $rs;
  }

    /**
     * @param $post
     */
  public function insert_team($post)
  {
      $res = Db::name('team')->insertGetId($post);
      if($res){
          return $res;
      }else{
          return false;
      }
  }

  /**
   * [getGscheduleListById 根据跟团游id获取班期列表]
   * @Author   kendy
   * @DateTime 2019-11-08T11:50:30+0800
   * @param    [type]                   $gt_id [description]
   * @return   [type]                          [description]
   */
   public function getGscheduleListById($tg_id)
   {
      $time = getNextMothLastDay(); 
      //跟团有产品截止到下个月底
      $where['tg_date']  = array('between','');  
      $resultData = Db::name('gtour_gschedule')
      ->field('team_id,tg_id,tg_date,tg_price')
      ->where('tg_id',$tg_id)
      ->where('tg_date','between time',[date('Y-m-d',time()),$time])
      ->order('tg_date asc')
      ->select();   
     return $resultData;
   }


}